<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class index extends MY_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct() {
        parent::__construct();
    }

    public function index() {



        parent::core_view('body_view'); //body
    }

    public function profile_user($id = false) {
        $data['refere'] = $this->input->server('HTTP_REFERER', TRUE);
        if ($id == false) {
            $id = $this->input->post('id');
        }
        $type_user = $this->input->post('type_user');

        $data = array('status' => $val);
        $table = $type_user . 's';
        if ($type_user == 'doctor') {
            $feild_id = 'doc_id';
            $data['topic_type'] = 'แพทย์';
            $pre = 'doc';
        } elseif ($type_user == 'staff') {
            $feild_id = 'sf_id';
            $data['topic_type'] = 'เจ้าหน้าที่';
            $pre = 'sf';
        } elseif ($type_user == 'tech') {
            $feild_id = 'tech_id';
            $data['topic_type'] = 'เจ้าหน้าที่ฝ่ายช่าง';
            $pre = 'tech';
        } elseif ($type_user == 'admin') {
            $table = 'users';
            $feild_id = 'user_id';
            $data['topic_type'] = 'ผู้ดูแลระบบ';
        }
        $profile = $this->general_model->Query1("select * from {$table} where {$feild_id} = {$id} limit 0,1");
        foreach ($profile as $key => $value) {
            $data['profile'][] = array(
                'id' => $value[$pre . '_id'],
                'name' => $value[$pre . '_name'] . "  " . $value[$pre . '_lname'],
                'birth' => $value[$pre . 'birth'],
                'address' => $value[$pre . 'address'],
                'tel' => $value[$pre . 'tel'],
                'email' => $value[$pre . 'email'],
            );
        }
        $data['login'] = $this->general_model->Query1("select * from users where user_id = {$data['profile'][0][$feild_id]} limit 0,1");
        $data['profile'][0] = array('type' => $type_user);

        $view = 'core_' . $this->usertype . '_view';
        parent::$view('profile', $data);
    }

    public function query_profile_user() {
        $data['refere'] = $this->input->server('HTTP_REFERER', TRUE);
        $id = $this->input->post('id');
        $type_user = $this->input->post('type_user');


        $data['profile'] = $this->general_model->Query1("select * from {$table} where {$feild_id} = {$id} limit 0,1");
        $data['login'] = $this->general_model->Query1("select * from users where user_id = {$data['profile'][0][$feild_id]} limit 0,1");
        $data = array('status' => $val);
        $table = $type_user . 's';
        if ($type_user == 'doctor') {
            $feild_id = 'doc_id';
            $pre = 'doc';
        } elseif ($type_user == 'staff') {
            $feild_id = 'sf_id';
            $pre = 'sf';
        } elseif ($type_user == 'tech') {
            $feild_id = 'tech_id';
            $pre = 'tech';
        } elseif ($type_user == 'admin') {
            $table = 'users';
            $feild_id = 'user_id';
        }
        if ($this->input->post('submit')) {
            $data_user = array(
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password'),
            );
            $this->general_model->update('users', $data_user, "user_id = {$data['login'][0]['user_id']}");

            if ($type_user != 'admin') {
                $data_profile = array(
                    $pre . '_name' => $this->input->post('name'),
                    $pre . '_lname' => $this->input->post('lname'),
                    $pre . '_birth' => $this->input->post('birth'),
                    $pre . '_address' => $this->input->post('address'),
                    $pre . '_tel' => $this->input->post('tel'),
                    $pre . '_email' => $this->input->post('email'),
                );
                $this->general_model->update($table, $data_profile, "{$feild_id} = {$id}")
                ?>
                <form action='<?php echo base_url() ?>index/profile_user' method='post' name='frm'>
                    <input type="hidden" name="type_user" value="<?php echo $type; ?>">
                    <input type="hidden" name="id" value="<?php echo $id; ?>">
                </form>
                <script language="JavaScript">
                    document.frm.submit();
                </script>
                <?php
            }
        }

        $view = 'core_' . $this->usertype . '_view';
        parent::$view('profile', $data);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */